package project.dade.cn.bst.views;

import android.app.Activity;
import android.content.Context;
import android.content.res.Configuration;
import android.os.Handler;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.TextView;

import project.dade.cn.bst.R;


/**
 * 自定义的Dialog,不可back或点击外部销毁
 * 
 * @author
 * 
 */
public class TipsDialog {
	public final static int SELECT_DIALOG = 1;
	public final static int RADIO_DIALOG = 2;

	/**
	 * 创建一个内容多选对话框
	 * 
	 * @param context
	 * @param title
	 *            标题
	 * @param items
	 *            数组
	 * @param dialogItemClickListener
	 *            监听点击的内容结果
	 * @return
	 */
	public static android.app.Dialog showListDialog(Context context,
			String title, String[] items,
			final DialogItemClickListener dialogItemClickListener) {
		return ShowDialog(context, title, items, dialogItemClickListener);
	}

	/**
	 * 创建一个单选对话框
	 * 
	 * @param context
	 * @param toast
	 *            提示消息
	 * @param dialogClickListener
	 *            点击监听
	 * @return
	 */
	public static android.app.Dialog showRadioDialog(Context context,
			String toast, final DialogClickListener dialogClickListener) {
		return ShowDialog(context,
				context.getResources().getString(R.string.pointMessage), toast,
				dialogClickListener, RADIO_DIALOG);
	}

	/**
	 * 创建一个选择对话框
	 * 
	 * @param context
	 * @param toast
	 *            提示消息
	 * @param dialogClickListener
	 *            点击监听
	 * @return
	 */
	public static android.app.Dialog showSelectDialog(Context context,
			String toast, final DialogClickListener dialogClickListener) {
		return ShowDialog(context,
				context.getResources().getString(R.string.pointMessage), toast,
				dialogClickListener, SELECT_DIALOG);
	}

	/**
	 * 自定义创建一个选择对话框
	 * 
	 * @param context
	 *            上下文
	 * @param tipTitle
	 *            提示标题
	 * @param tipMessage
	 *            提示信息
	 * @param tipCancel
	 *            提示取消文字(默认为取消)
	 * @param tipOk
	 *            提示确定文字（默认设置）
	 * @param dialogClickListener
	 * @return
	 */
	public static android.app.Dialog showCustomSelectDialog(Context context,
			String tipTitle, String tipMessage, String tipCancel, String tipOk,
			final DialogClickListener dialogClickListener) {
		return ShowCustomSelectDialog(context, tipTitle, tipMessage, tipCancel,
				tipOk, dialogClickListener, SELECT_DIALOG);
	}

	/**
	 * 创建一个选择对话框
	 * 
	 * @param context
	 * @param title
	 *            提示标题
	 * @param toast
	 *            提示消息
	 * @param dialogClickListener
	 *            点击监听
	 * @return
	 */
	public static android.app.Dialog showSelectDialog(Context context,
			String title, String toast,
			final DialogClickListener dialogClickListener) {
		return ShowDialog(context, title, toast, dialogClickListener,
				SELECT_DIALOG);
	}

	private static android.app.Dialog ShowDialog(Context context, String title,
			String toast, final DialogClickListener dialogClickListener,
			int DialogType) {
		final android.app.Dialog dialog = new android.app.Dialog(context,
				R.style.DialogStyle);
		dialog.setCancelable(false);
		View view = LayoutInflater.from(context).inflate(R.layout.tipdialog,
				null);
		dialog.setContentView(view);
		((TextView) view.findViewById(R.id.point)).setText(title);
		((TextView) view.findViewById(R.id.toast)).setText(toast);
		if (DialogType == RADIO_DIALOG) {
		} else {
			view.findViewById(R.id.ok).setVisibility(View.GONE);
			view.findViewById(R.id.divider).setVisibility(View.VISIBLE);
		}
		view.findViewById(R.id.cancel).setOnClickListener(
				new OnClickListener() {
					@Override
					public void onClick(View arg0) {
						dialog.dismiss();
						new Handler().postDelayed(new Runnable() {
							@Override
							public void run() {
								dialogClickListener.cancel();
							}
						}, 200);
					}
				});
		view.findViewById(R.id.confirm).setOnClickListener(
				new OnClickListener() {
					@Override
					public void onClick(View arg0) {
						dialog.dismiss();
						new Handler().postDelayed(new Runnable() {
							@Override
							public void run() {
								dialogClickListener.confirm();
							}
						}, 200);
					}
				});
		view.findViewById(R.id.ok).setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				dialog.dismiss();
				new Handler().postDelayed(new Runnable() {
					@Override
					public void run() {
						dialogClickListener.confirm();
					}
				}, 200);
			}
		});
		Window mWindow = dialog.getWindow();
		WindowManager.LayoutParams lp = mWindow.getAttributes();
		if (context.getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {// 横屏
			lp.width = getScreenHeight(context) / 10 * 8;
		} else {
			lp.width = getScreenWidth(context) / 10 * 8;
		}
		mWindow.setAttributes(lp);
		dialog.show();

		return dialog;
	}

	/**
	 * 自定义选择对话框
	 * 
	 * @param context
	 * @param tipTitle
	 * @param tipMessage
	 * @param tipCancel
	 * @param tipOk
	 * @param dialogClickListener
	 * @param DialogType
	 * @return
	 */
	private static android.app.Dialog ShowCustomSelectDialog(Context context,
			String tipTitle, String tipMessage, String tipCancel, String tipOk,
			final DialogClickListener dialogClickListener, int DialogType) {
		final android.app.Dialog dialog = new android.app.Dialog(context,
				R.style.DialogStyle);
		dialog.setCancelable(false);
		View view = LayoutInflater.from(context).inflate(R.layout.tipdialog,
				null);
		dialog.setContentView(view);
		((TextView) view.findViewById(R.id.point)).setText(tipTitle);
		((TextView) view.findViewById(R.id.toast)).setText(tipMessage);
		((TextView) view.findViewById(R.id.confirm)).setText(tipOk);
		if (DialogType == RADIO_DIALOG) {
		} else {
			view.findViewById(R.id.ok).setVisibility(View.GONE);
			view.findViewById(R.id.divider).setVisibility(View.VISIBLE);
		}
		view.findViewById(R.id.cancel).setOnClickListener(
				new OnClickListener() {
					@Override
					public void onClick(View arg0) {
						dialog.dismiss();
						new Handler().postDelayed(new Runnable() {
							@Override
							public void run() {
								dialogClickListener.cancel();
							}
						}, 200);
					}
				});
		view.findViewById(R.id.confirm).setOnClickListener(
				new OnClickListener() {
					@Override
					public void onClick(View arg0) {
						dialog.dismiss();
						new Handler().postDelayed(new Runnable() {
							@Override
							public void run() {
								dialogClickListener.confirm();
							}
						}, 200);
					}
				});
		view.findViewById(R.id.ok).setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				dialog.dismiss();
				new Handler().postDelayed(new Runnable() {
					@Override
					public void run() {
						dialogClickListener.confirm();
					}
				}, 200);
			}
		});
		Window mWindow = dialog.getWindow();
		WindowManager.LayoutParams lp = mWindow.getAttributes();
		if (context.getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {// 横屏
			lp.width = getScreenHeight(context) / 10 * 8;
		} else {
			lp.width = getScreenWidth(context) / 10 * 8;
		}
		mWindow.setAttributes(lp);
		dialog.show();

		return dialog;
	}

	private static android.app.Dialog ShowDialog(Context context, String title,
			String[] items, final DialogItemClickListener dialogClickListener) {
		final android.app.Dialog dialog = new android.app.Dialog(context,
				R.style.DialogStyle);
		dialog.setCancelable(false);
		View view = LayoutInflater.from(context).inflate(
				R.layout.tipdialog_radio, null);
		dialog.setContentView(view);
		((TextView) view.findViewById(R.id.title)).setText(title);
		// 根据items动态创建
		LinearLayout parent = (LinearLayout) view
				.findViewById(R.id.dialogLayout);
		parent.removeAllViews();
		int length = items.length;
		for (int i = 0; i < items.length; i++) {
			LayoutParams params1 = new LayoutParams(-1, -2);
			params1.rightMargin = 1;
			final TextView tv = new TextView(context);
			tv.setLayoutParams(params1);
			tv.setTextSize(18);
			tv.setText(items[i]);
			tv.setTextColor(context.getResources().getColor(
					R.color.dialogTxtColor));
			int pad = context.getResources().getDimensionPixelSize(
					R.dimen.common_dimens_padding);
			tv.setPadding(pad, pad, pad, pad);
			tv.setSingleLine(true);
			tv.setGravity(Gravity.CENTER);
			if (i != length - 1)
				tv.setBackgroundResource(R.drawable.menudialog_center_selector);
			else
				tv.setBackgroundResource(R.drawable.menudialog_bottom2_selector);

			tv.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View arg0) {
					dialog.dismiss();
					dialogClickListener.confirm(tv.getText().toString());
				}
			});
			parent.addView(tv);
			if (i != length - 1) {
				TextView divider = new TextView(context);
				LayoutParams params = new LayoutParams(-1, (int) 1);
				divider.setLayoutParams(params);
				divider.setBackgroundResource(android.R.color.darker_gray);
				parent.addView(divider);
			}
		}
		view.findViewById(R.id.ok).setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				dialog.dismiss();
			}
		});
		Window mWindow = dialog.getWindow();
		WindowManager.LayoutParams lp = mWindow.getAttributes();
		lp.width = getScreenWidth(context);
		mWindow.setGravity(Gravity.BOTTOM);
		// 添加动画
		mWindow.setWindowAnimations(R.style.dialogAnim);
		mWindow.setAttributes(lp);
		dialog.show();
		return dialog;
	}

	/** 获取屏幕分辨率宽 */
	public static int getScreenWidth(Context context) {
		DisplayMetrics dm = new DisplayMetrics();
		((Activity) context).getWindowManager().getDefaultDisplay()
				.getMetrics(dm);
		return dm.widthPixels;
	}

	/** 获取屏幕分辨率高 */
	public static int getScreenHeight(Context context) {
		DisplayMetrics dm = new DisplayMetrics();
		((Activity) context).getWindowManager().getDefaultDisplay()
				.getMetrics(dm);
		return dm.heightPixels;
	}

	public interface DialogClickListener {
		public abstract void confirm();

		public abstract void cancel();
	}

	public interface DialogItemClickListener {
		public abstract void confirm(String result);
	}
}